package com.ruoyi.web.controller.wms;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsMaterialCategory;
import com.ruoyi.wms.service.IWmsMaterialCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料类别树Controller
 * 
 * @author 杨懿
 * @date 2023-11-27
 */
@Controller
@RequestMapping("/wms/materialCategory")
public class WmsMaterialCategoryController extends BaseController
{
    private String prefix = "wms/materialCategory";

    @Autowired
    private IWmsMaterialCategoryService wmsMaterialCategoryService;

    @RequiresPermissions("wms:materialCategory:view")
    @GetMapping()
    public String materialCategory()
    {
        return prefix + "/materialCategory";
    }

    /**
     * 查询物料类别树列表
     */
    @RequiresPermissions("wms:materialCategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<WmsMaterialCategory> list(WmsMaterialCategory dept)
    {
        if(!ShiroUtils.getSysUser().isAdmin()){
            dept.setParentId(ShiroUtils.getSysUser().getDeptId());
        }
        List<WmsMaterialCategory> deptList = wmsMaterialCategoryService.selectWmsMaterialCategoryList(dept);
        return deptList;
    }

    /**
     * 导出物料类别树列表
     */
    @RequiresPermissions("wms:materialCategory:export")
    @Log(title = "物料类别树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsMaterialCategory wmsMaterialCategory)
    {
        if(!ShiroUtils.getSysUser().isAdmin()){
            wmsMaterialCategory.setParentId(ShiroUtils.getSysUser().getDeptId());
        }
        List<WmsMaterialCategory> list = wmsMaterialCategoryService.selectWmsMaterialCategoryList(wmsMaterialCategory);
        ExcelUtil<WmsMaterialCategory> util = new ExcelUtil<WmsMaterialCategory>(WmsMaterialCategory.class);
        return util.exportExcel(list, "物料类别树数据");
    }

    /**
     * 新增物料类别树
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        if (!getSysUser().isAdmin())
        {
            parentId = getSysUser().getDeptId();
        }
        mmap.put("dept", wmsMaterialCategoryService.selectWmsMaterialCategoryByDeptId(parentId));
        return prefix + "/add";
    }
    /**
     * 新增保存物料类别树
     */
    @RequiresPermissions("wms:materialCategory:add")
    @Log(title = "物料类别树", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsMaterialCategory wmsMaterialCategory)
    {
        return toAjax(wmsMaterialCategoryService.insertWmsMaterialCategory(wmsMaterialCategory));
    }

    /**
     * 修改物料类别树
     */
    @RequiresPermissions("wms:materialCategory:edit")
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {

        WmsMaterialCategory dept = wmsMaterialCategoryService.selectWmsMaterialCategoryByDeptId(deptId);

        mmap.put("dept", dept);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料类别树
     */
    @RequiresPermissions("wms:materialCategory:edit")
    @Log(title = "物料类别树", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsMaterialCategory wmsMaterialCategory)
    {
        return toAjax(wmsMaterialCategoryService.updateWmsMaterialCategory(wmsMaterialCategory));
    }

    /**
     * 删除物料类别树
     */
    @RequiresPermissions("wms:materialCategory:remove")
    @Log(title = "物料类别树", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsMaterialCategoryService.deleteWmsMaterialCategoryByDeptIds(ids));
    }
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId)
    {
        if (wmsMaterialCategoryService.selectDeptCount(deptId) > 0)
        {
            return AjaxResult.warn("存在下级仓库,不允许删除");
        }

        return toAjax(wmsMaterialCategoryService.deleteWmsMaterialCategoryByDeptId(deptId));
    }
    /**
     * 选择部门树
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectDeptTree/{deptId}", "/selectDeptTree/{deptId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("deptId") Long deptId,
                                 @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap)
    {
        mmap.put("dept", wmsMaterialCategoryService.selectWmsMaterialCategoryByDeptId(deptId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        WmsMaterialCategory tem = new WmsMaterialCategory();
        if(!ShiroUtils.getSysUser().isAdmin()){
            tem.setParentId(ShiroUtils.getSysUser().getDeptId());
        }
        List<Ztree> ztrees = wmsMaterialCategoryService.selectDeptTree(tem);
        return ztrees;
    }
    @GetMapping("/treeData1")
    @ResponseBody
    public List<Ztree> treeData1()
    {
        WmsMaterialCategory tem = new WmsMaterialCategory();
        tem.setParentId(100l);
        List<Ztree> ztrees = wmsMaterialCategoryService.selectDeptTree(tem);
        return ztrees;
    }
    /**
     * 加载部门列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId)
    {
        WmsMaterialCategory dept = new WmsMaterialCategory();
        if(!ShiroUtils.getSysUser().isAdmin()){
            dept.setParentId(ShiroUtils.getSysUser().getDeptId());
        }
        dept.setExcludeId(excludeId);
        List<Ztree> ztrees = wmsMaterialCategoryService.selectDeptTreeExcludeChild(dept);
        return ztrees;
    }

}
